#!/bin/bash

echo "=== 测试跟进API增删改查全流程 ==="

# 登录获取token
echo "1. 登录获取token..."
LOGIN_RESPONSE=$(curl -s -X POST http://127.0.0.1:8000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"liuhui","password":"123456"}')

echo "登录响应: $LOGIN_RESPONSE"

# 提取token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
echo "Token: $TOKEN"

# 设置请求头
HEADERS="-H 'Content-Type: application/json' -H 'Authorization: Bearer $TOKEN'"

echo ""
echo "================================"
echo "2. 测试获取跟进选项接口..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/getFollowupOptions" | jq '.'

echo ""
echo "================================"
echo "3. 测试获取跟进类型选项..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/typeOptions" | jq '.'

echo ""
echo "================================"
echo "4. 测试创建常规跟进（opportunity_id=0）..."
echo "================================"
CREATE_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/create" \
  -d '{
    "customer_id": 1,
    "opportunity_id": 0,
    "followup_type": 1,
    "followup_time": "2025-01-15 14:30:00",
    "content": "这是测试的常规跟进记录"
  }')

echo "创建跟进响应: $CREATE_RESPONSE"

# 提取跟进ID
FOLLOWUP_ID=$(echo $CREATE_RESPONSE | grep -o '"id":[0-9]*' | cut -d':' -f2)
echo "跟进ID: $FOLLOWUP_ID"

echo ""
echo "================================"
echo "5. 测试创建机会跟进（opportunity_id>0）..."
echo "================================"
CREATE_OPP_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/create" \
  -d '{
    "customer_id": 1,
    "opportunity_id": 1,
    "followup_type": 2,
    "followup_time": "2025-01-16 10:00:00",
    "content": "这是测试的机会跟进记录"
  }')

echo "创建机会跟进响应: $CREATE_OPP_RESPONSE"

echo ""
echo "================================"
echo "6. 测试获取跟进列表..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/list?page=1&limit=10" | jq '.'

if [ -n "$FOLLOWUP_ID" ]; then
  echo ""
  echo "================================"
  echo "7. 测试获取跟进详情..."
  echo "================================"
  curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/detail?id=$FOLLOWUP_ID" | jq '.'

  echo ""
  echo "================================"
  echo "8. 测试编辑跟进..."
  echo "================================"
  EDIT_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/edit" \
    -d "{
      \"id\": $FOLLOWUP_ID,
      \"content\": \"这是编辑后的跟进内容\",
      \"followup_type\": 3
    }")

  echo "编辑跟进响应: $EDIT_RESPONSE"

  echo ""
  echo "================================"
  echo "9. 测试删除跟进..."
  echo "================================"
  DELETE_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/del" \
    -d "{\"id\": $FOLLOWUP_ID}")

  echo "删除跟进响应: $DELETE_RESPONSE"
fi

echo ""
echo "================================"
echo "10. 再次获取跟进列表（验证删除）..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/list?page=1&limit=10" | jq '.'

echo ""
echo "=== 测试完成 ==="
